
package com.example.chiro;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DB_FICHEARBRE_adapter {
    public static final String DATABASE_NAME = "fiche_arbre"; 
    public static final int DATABASE_VERSION = 1;

    private static final String CREATE_TABLE_DONNEES_GENERAL =
            "create table "+Table_general.DATABASE_TABLE+" (_id integer primary key autoincrement, " 
                    + Table_general.DATE + " TEXT," 
                    + Table_general.NOM + " TEXT," 
                    + Table_general.PRENOM + " TEXT," 
                    + Table_general.LONGITUDE + " TEXT," 
                    + Table_general.LATITUDE + " TEXT," 
                    + Table_general.ALTITUDE + " TEXT," 
                    + Table_general.COMMUNE + " TEXT DEFAULT \'\',"	//La commune et lieu-dit seront calculés lors de la synchonisation sqlite/remote database
                    + Table_general.LIEU_DIT + " TEXT DEFAULT \'\'," 
                    + Table_general.ENVOYE + " TEXT DEFAULT \'false\' " + ");"; //valeur qui deviendra vrai une fois la données envoyée avec succès vers la remote database
    
    private static final String CREATE_TABLE_DONNEES_ARBRE =
            "create table "+Table_arbre.DATABASE_TABLE+" (_id integer primary key autoincrement, " 
                    + Table_arbre.PATH_PHOTO + " TEXT," 
                    + Table_arbre.TYPE + " TEXT," 
                    + Table_arbre.HAUTEUR + " TEXT," 
                    + Table_arbre.DIAMETRE + " TEXT," 
                    + Table_arbre.STATUT + " TEXT," 
                    + Table_arbre.ETAT + " TEXT," 
                    + Table_arbre.PEUPLEMENT + " TEXT,"
                    + Table_arbre.HOUPPIER + " TEXT,"
                    + Table_arbre.POTENTIEL + " TEXT,"
    				+ Table_arbre.ENVOYE + " TEXT DEFAULT \'false\' " + ");"; 
    
    private static final String CREATE_TABLE_DONNEES_CAVITE =
            "create table "+Table_cavite.DATABASE_TABLE+" (_id integer primary key autoincrement, " 
                    + Table_cavite.ID_FICHE + " TEXT," 
                    + Table_cavite.PATH_PHOTO + " TEXT," 
                    + Table_cavite.TYPE + " TEXT," 
                    + Table_cavite.FIBROSCOPE + " TEXT," 
                    + Table_cavite.COULURE + " TEXT," 
                    + Table_cavite.GUANO + " TEXT,"  
                    + Table_cavite.POSITION + " TEXT,"
    				+ Table_cavite.ENVOYE + " TEXT DEFAULT \'false\' " + ");"; 
    
    private static final String CREATE_TABLE_DONNEES_ESPECE =
            "create table "+Table_espece.DATABASE_TABLE+" (_id integer primary key autoincrement, " 
                    + Table_espece.ID_FICHE + " TEXT," 
                    + Table_espece.PATH_PHOTO + " TEXT," 
                    + Table_espece.NOM + " TEXT," 
                    + Table_espece.EFFECTIF + " TEXT," 
                    + Table_espece.RARETE + " TEXT,"
                    + Table_espece.COMMENTAIRE + " TEXT,"
    				+ Table_espece.ENVOYE + " TEXT DEFAULT \'false\' " + ");";
    				
    private final Context context;
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DB_FICHEARBRE_adapter(Context ctx)
    {
        this.context = ctx;
        this.DBHelper = new DatabaseHelper(this.context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context)
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            db.execSQL(CREATE_TABLE_DONNEES_GENERAL);
            db.execSQL(CREATE_TABLE_DONNEES_ARBRE);
            db.execSQL(CREATE_TABLE_DONNEES_CAVITE);
            db.execSQL(CREATE_TABLE_DONNEES_ESPECE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion,
                int newVersion)
        {
        }
    }
    
    public DB_FICHEARBRE_adapter open() throws SQLException
    {
        this.db = this.DBHelper.getWritableDatabase();
        return this;
    }

    public void close()
    {
        this.DBHelper.close();
    }
}
